/**
 * 时间戳
 * @param {*} timestamp  时间戳
 */
 const timestampToTime = (timestamp) => {
    let date = new Date(timestamp) //   时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D =
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
    let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s;
};

const timestampToTimeSa = (timestamp) => {
    let date = new Date(timestamp) //   时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D =
        (date.getDate() < 10 ? '0' + (date.getDate() + 1) : (date.getDate()) + 1) + ' '
    let h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
    let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s;
};

// 年月日
const timestampYmd = (timestamp) => {
	let date = new Date(timestamp) //   时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let Y = date.getFullYear() + '-'
	let M =
			(date.getMonth() + 1 < 10 ?
					'0' + (date.getMonth() + 1) :
					date.getMonth() + 1) + '-'
	let D =
			(date.getDate() < 10 ? '0' + (date.getDate()) : (date.getDate()));
	return Y + M + D;
};

// 获取月
const timestampMath = (timestamp) => {
	let date = new Date(timestamp) //   时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let Y = date.getFullYear() + '-'
	let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	return Y + M + '-01';
};

// 获取年
const timestampYears = (timestamp) => {
	let date = new Date(timestamp) //   时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let Y = date.getFullYear();
	return Y + '-01' + '-01' ;
};

/*
    转换为时分秒
*/
const timestampToTimeShi = (timestamp) => {
    var secondTime = parseInt(timestamp); // 秒
    var minuteTime = 0; // 分
    var hourTime = 0; // 小时
    if (secondTime >= 60) {
        minuteTime = parseInt(secondTime / 60);
        secondTime = parseInt(secondTime % 60);
        if (minuteTime >= 60) {
            hourTime = parseInt(minuteTime / 60);
            minuteTime = parseInt(minuteTime % 60);
        }
    }
    var result = "" +(parseInt(secondTime) < 10 ? "0" + parseInt(secondTime): parseInt(secondTime)) + '秒';
    if (minuteTime > 0) { result = "" + (parseInt(minuteTime) < 10 ? "0" + parseInt(minuteTime) : parseInt(minuteTime)) + "分" + result; }
    if (hourTime > 0) {result = "" + (parseInt(hourTime) < 10 ? "0" + parseInt(hourTime) : parseInt(hourTime)) +"时" + result; }
    return result;
};

// 转换年月日
const timestampToTimes = (timestamp) => {
    let date = new Date(timestamp) //   时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
    let D = (date.getDate() < 10  ? '0' + (date.getDate() - 1) : (date.getDate() - 1));
    return Y + M + D ;
};
// 转换年月日
const timestampToTimesEs = (timestamp) => {
    let date = new Date(timestamp) //   时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
    return Y + M + D ;
};

/*
*后台返回的秒 转为分秒形式
*/
const conversionTime = (timeTime) => {
    // var hours = Math.floor((timeTime % 86400) / 3600);
    var minutes = Math.ceil(((timeTime % 86400) % 3600) / 60);
    var seconds = Math.ceil(((timeTime % 86400) % 3600) % 60);
    // if(h < 10) { h = '0' + h }
    if(minutes < 10) { minutes = '0' + minutes }
    if(seconds < 10) { seconds = '0' + seconds }
    var duration = minutes + "分" + seconds + "秒";
    // var duration = hours + "小时" + minutes + "分" + seconds + "秒";
    // console.log(duration);
    return duration;
}

/*
秒转 分钟
*/ 
const minutesStudents = (timeTime) => {
    var minutes = Math.abs(timeTime / 60).toFixed(1);
    // console.log(minutes)
    // if(minutes < 10) { minutes = '0' + minutes }
    var duration = minutes;
    return duration;
}

/*
  时间秒转为时
*/
const mTimeSeconds = (timeTime) => {
    var hours = parseFloat(timeTime / 3600).toFixed(2);
    // if(hours < 10) { hours = '0' + hours }
    // console.log(hours);
    return hours;
}

/**
 * 存储localStorage
 */
const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
const removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}

/**
 * 设置cookie
 **/
function setCookie(name, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
};

/**
 * 获取cookie
 **/
function getCookie(name) {
    let reg = RegExp(name + '=([^;]+)');
    let arr = document.cookie.match(reg);
    if (arr) {
        return arr[1];
    } else {
        return '';
    }
};

/**
 * 删除cookie
 **/
function delCookie(name) {
    setCookie(name, null, -1);
};

/**
 * 导出 
 **/
export {
	timestampToTime,
	timestampToTimeShi,
	timestampToTimes,
    timestampToTimesEs,
	timestampToTimeSa,
	timestampMath,
	timestampYears,
	timestampYmd,
	conversionTime,
	minutesStudents,
	mTimeSeconds,
	setStore,
	getStore,
	removeStore,
	setCookie,
	getCookie,
	delCookie,
}